package com.sxzjrj.utils

import org.apache.spark.rdd.RDD
import scalikejdbc.{DB, SQL}
import scalikejdbc.config.DBs

/**
  * Created by ljj in 2019/3/20
  *
  */
object JdbcUtils {

  DBs.setup()

  def getData4Mysql = {

    DB.localTx{
      implicit session =>{

        println(SQL(
          """
            |select * from t_cmcc_test where 1=1 limit 10
          """.stripMargin)
          .update().apply())
      }
    }



  }

  def saveData2Mysql(result: RDD[(String, (Int, Int, Int, Long))]) = {

    result.foreachPartition(iter => {
      DB.localTx{
        implicit session =>
          iter.foreach(tp => {
            SQL(
              """
                |insert into t_cmcc_core Value(?,?,?,?,?)
              """.stripMargin)
              .bind(tp._1,tp._2._1,tp._2._2,tp._2._3,tp._2._4)
              .update()
              .apply()
          })
      }
    })

  }

}
